//
//  VerticalInfiniteScrollView.m
//  RBIOSBase
//
//  Created by ren on 08/12/2017.
//  Copyright © 2017 bryan. All rights reserved.
//

#import "VerticalInfiniteScrollView.h"

@interface VerticalInfiniteScrollView ()

@property (nonatomic, strong) UILabel *firstLabel;
@property (nonatomic, strong) UILabel *secondLabel;
@property (nonatomic, strong) UILabel *thirdLabel;

@end

@implementation VerticalInfiniteScrollView {
	NSInteger pageNumber;
	NSInteger autoScrollNumber;
	BOOL autoScrollLabel;
}

- (instancetype)initWithFrame:(CGRect)frame
{
	if (self = [super initWithFrame:frame]) {
		self.delegate = self;
		self.contentSize = CGSizeMake(self.frame.size.width, self.frame.size.height * 3);
		self.showsVerticalScrollIndicator = NO;
		self.showsHorizontalScrollIndicator = NO;
		self.pagingEnabled = YES;
		self.backgroundColor = [UIColor redColor];
	}
	return self;
}

- (void)setScrollStyle
{
	[self setScrollStyle:NO];
}

- (void)setScrollStyle:(BOOL)autoScroll
{
	autoScrollLabel = autoScroll;
	autoScrollNumber = 0;
	pageNumber = 0;
	[self addLabels];
	[self showLabels];
	if (autoScroll) {
		[self createTimer];
	}
}

- (void)createTimer
{
	if (!curTimer) {
		curTimer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(excellent) userInfo:nil repeats:YES];
		
	}
	[[NSRunLoop mainRunLoop] addTimer:curTimer forMode:NSRunLoopCommonModes];
	
}

- (void)excellent
{
	autoScrollNumber ++;
	if (autoScrollNumber >= 2) {
		autoScrollNumber = 2;
	}
	[self scrollRectToVisible:CGRectMake(0, self.bounds.size.height * autoScrollNumber, self.bounds.size.width, self.bounds.size.height) animated:YES];
}

- (void)showLabels
{
	if (self.titleList && [self.titleList count] > 1) {
		[self.firstLabel setText:[self.titleList objectAtIndex:pageNumber]];
		NSInteger number = (pageNumber + 1) >= [self.titleList count] ? 0 : (pageNumber + 1);
		[self.secondLabel setText:[self.titleList objectAtIndex:number]];
		
		NSInteger number1 = 0;
		if (number == 0) {
			number1 = 1;
		}
		else {
			number1 = (pageNumber + 2) >= [self.titleList count] ? 0 : (pageNumber + 2);
		}
		[self.thirdLabel setText:[self.titleList objectAtIndex:number1]];
	}
	
}

- (void)addLabels
{
	for (UIView *subView in self.subviews) {
		[subView removeFromSuperview];
	}
	if (self.titleList && [self.titleList count] == 1) {
		[self addSubview:self.firstLabel];
	}
	else if (self.titleList && [self.titleList count] > 1) {
		[self addSubview:self.firstLabel];
		[self addSubview:self.secondLabel];
		[self addSubview:self.thirdLabel];
	}
}

#pragma mark - getter

- (UILabel *)firstLabel
{
	if (!_firstLabel) {
		_firstLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
		[_firstLabel setTextColor:[UIColor whiteColor]];
	}
	return _firstLabel;
}

- (UILabel *)secondLabel
{
	if (!_secondLabel) {
		_secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.bounds.size.height, self.bounds.size.width, self.bounds.size.height)];
		[_secondLabel setTextColor:[UIColor whiteColor]];
	}
	return _secondLabel;
}

- (UILabel *)thirdLabel
{
	if (!_thirdLabel) {
		_thirdLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.bounds.size.height * 2, self.bounds.size.width, self.bounds.size.height)];
		[_thirdLabel setTextColor:[UIColor whiteColor]];
	}
	return _thirdLabel;
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
	if (scrollView.contentOffset.y >= self.bounds.size.height * 2) {
		pageNumber ++;
		if (self.titleList && pageNumber >= [self.titleList count]) {
			pageNumber = 0;
		}
		[self showLabels];
		[self scrollRectToVisible:CGRectMake(0, self.bounds.size.height, self.bounds.size.width, self.bounds.size.height) animated:NO];
	}
	else if (scrollView.contentOffset.y <= 0) {
		pageNumber --;
		if (self.titleList && pageNumber < 0) {
			pageNumber = [self.titleList count] - 1;
		}
		[self showLabels];
		[self scrollRectToVisible:CGRectMake(0, self.bounds.size.height, self.bounds.size.width, self.bounds.size.height) animated:NO];
	}
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
	if (autoScrollLabel) {
		if (curTimer) {
			[curTimer invalidate];
			curTimer = nil;
		}
	}
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
	if (autoScrollLabel) {
		[self createTimer];
	}
}

@end
